<?php
mysql_connect('localhost', 'taras-race', 'mhxzkhl')  or die("Could not connect: " . mysql_error());
mysql_select_db('taras_race');
mysql_query('set names "uft8"');

$players = mysql_query('select distinct(player_id) from owner');

if (!$players) {
  echo("failed to get player IDs from owner table");
   return;
}

function get_follower_count($player, $day) {
    $sql = sprintf("select sum(followed_count) as count
                    from owner, user_statistic
                    where
                      locate(owner.account, user_statistic.user) > 0
                      and
                      owner.player_id = '%s'
                      and
                      collect_date = '%s'", $player, $day);
    $ret = mysql_fetch_assoc(mysql_query($sql));
    return $ret['count'];
}

function gather_rank_info($player) {
    $info = array();

    $info[] = $player;
    $info[] = get_follower_count($player, date('Y-m-d', strtotime('today')));
    $info[] = get_follower_count($player, date('Y-m-d', strtotime('-1 day')));
    $info[] = get_follower_count($player, date('Y-m-d', strtotime('-2 day')));
    $info[] = get_follower_count($player, date('Y-m-d', strtotime('-7 day')));

    return $info;

    $sql = sprintf("select sum(followed) as followed,
                  sum(following) as following,
                  sum(tweet) as tweet 
                  from
                  (select account, max(followed_count) as followed,
                  max(follow_count) as following, max(tweet_count) as tweet
                  from owner, user_statistic where locate(owner.account, user_statistic.user) > 0
                      and 
                      owner.player_id='%s' group by account) as tmp", $player);
    $ret = mysql_fetch_assoc(mysql_query($sql));
    return array('player'=>$player, 
                 'followed'=>$ret['followed'],
                 'following'=>$ret['following'],
                 'tweet'=>$ret['tweet']);
  
}

function compare_player($a, $b) {
  return $a[1] < $b[1];
}

?>
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html;charset=utf-8">
    <link rel="stylesheet" type="text/css" href="http://yui.yahooapis.com/3.3.0/build/cssreset/reset-min.css">
    <title>Follower Race Rank | Taras Team Internal</title>

    <script src="/lib/js/jquery.js"></script>
    <script src="/lib/js/sprintf.js"></script>

    <script>
    $(document).ready(function() {
        $("#rank-table tr").each(function (index, tr) {
            if (index > 0) {
                size = $(tr).find('td').size();
                for (var i = 1; i < size - 1; i++) {
                    var cell = $(tr).find('td').eq(i);
                    var next_cell = $(tr).find('td').eq(i+1); 
                    var delta =  $(cell).html() - $(next_cell).html();
                    $(cell).append(sprintf(" <span class='delta'>%s</span>", delta));
                }
            }
        });

        $('span.delta').each(function(index, item) {
            if ($(item).html() > 0) {
                $(item).css('color', 'red');
            } else {
                $(item).css('color', 'green');
            }
            $(item).css('font-size', '12px');
        });
    });
    </script>

  </head>
  <body>
  <?php 
  while($player = mysql_fetch_row($players)) {
    $rank_info[] = gather_rank_info($player[0]);
  }


uasort($rank_info, 'compare_player');
$row = "<tr>
          <td>用户名</td> 
          <td>" . date('Y-m-d', strtotime('today')) . "</td> 
          <td>" . date('Y-m-d', strtotime('-1 day')) . "</td> 
          <td>" . date('Y-m-d', strtotime('-2 day')) . "</td> 
          <td>" . date('Y-m-d', strtotime('-7 day')) . "</td> 
          </tr>";
foreach ($rank_info as $info) {
  $row .= " <tr> 
          <td>{$info[0]}</td> 
          <td>{$info[1]}</td> 
          <td>{$info[2]} </td> 
          <td>{$info[3]}</td> 
          <td>{$info[4]}</td> 
          </tr> ";
}
echo "<center><table id='rank-table'>$row</table></center>";
  ?>


<style>
  #rank-table {
      border-collapse: collapse;
      margin: 40px;
  }

  #rank-table td {
      border: solid 1px #cdcdcd;
      padding: 5px;
  }
</style>


  </body>
</html>
